package com.jeeplus.test.jlnu.admissionInfo.service.impl;

import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

@Service
public class UploadProgressService {

    private final ConcurrentHashMap<String, AtomicLong> progressMap = new ConcurrentHashMap<>();

    public void startUpload(String fileId) {
        progressMap.put(fileId, new AtomicLong(0));
    }

    public void updateProgress(String fileId, long progress) {
        progressMap.computeIfPresent(fileId, (id, oldProgress) -> {
            oldProgress.set(progress);
            return oldProgress;
        });
    }

    public long getProgress(String fileId) {
        return progressMap.getOrDefault(fileId, new AtomicLong(0)).get();
    }

    public void finishUpload(String fileId) {
        progressMap.remove(fileId);
    }
}